package com.jiaz.untils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiaz.dto.PostionScoreDto;
import com.jiaz.dto.RecommendDto;
import com.jiaz.entity.Postion;
import com.jiaz.mapper.PostionMapper;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * @author jiajiazi
 * @version 1.0
 * @date 2022/5/11 18:16
 */
public class ResultWrap {

    public static List<RecommendDto> userWrap(PostionScoreDto[] data, int vennueId, PostionMapper postionMapper) {
        ArrayList<RecommendDto> res = new ArrayList<>();
        int k = data.length;
        double sum = 0;
        ArrayList<Integer> ids = new ArrayList<>();
        for (int i = 0; i < k; i++) {
            sum += data[i].getScore();
            ids.add(data[i].getVenueId());
        }
        Postion postion = postionMapper.selectById(vennueId);
        double lat = postion.getLat() / 180;
        double lon = postion.getLon() / 180;
        Arrays.sort(data, new Comparator<PostionScoreDto>() {
            @Override
            public int compare(PostionScoreDto o1, PostionScoreDto o2) {
                return o1.getVenueId() - o2.getVenueId();
            }
        });

        QueryWrapper<Postion> q = new QueryWrapper<>();
        q.in("id", ids).orderByAsc("id");
        List<Postion> postionList = postionMapper.selectList(q);
        for (int i = 0; i < k; i++) {
            Postion p = postionList.get(i);
            double lat1 = p.getLat();
            double lon1 = p.getLon();
            double d = (2.0 * 6371.0 * Math.asin(Math.sqrt(Math.pow(Math.sin((lat - lat1 / 180) / 2.0), 2) + Math.cos(lat) * Math.cos(lat1 / 180) * Math.pow(Math.sin((lon - lon1 / 180) / 2.0), 2))));

            res.add(new RecommendDto(p.getId(), p.getVenueCategory(), lat1, lon1, d, data[i].getScore() / sum, p.getFrequency()));
        }
        res.sort(new Comparator<RecommendDto>() {
            @Override
            public int compare(RecommendDto o1, RecommendDto o2) {
                double v = o2.getScore() - o1.getScore();
                return v == 0 ? 0 : v > 0 ? 1 : -1;
            }
        });
        return res;
    }

    public static double distant(double lat, double lon, double lat1, double lon1) {
        lat = lat / 180.0;
        lat1 = lat1 / 180.0;
        lon = lon / 180.0;
        lon1 = lon1 / 180.0;
        return 2.0 * 6371.0 * Math.asin(Math.sqrt(Math.pow(Math.sin((lat - lat1) / 2.0), 2) + Math.cos(lat) * Math.cos(lat1) * Math.pow(Math.sin((lon - lon1) / 2.0), 2)));

    }
}
